package com.kiln.juxingqs.ui

import android.view.View
import androidx.lifecycle.lifecycleScope
import client.global.commlib.toast.toast
import client.global.commlib.widget.EmptyLayout
import com.kiln.juxingqs.BaseMiddleActivity
import com.kiln.juxingqs.R
import com.kiln.juxingqs.adapter.WdtjAdapter
import com.kiln.juxingqs.databinding.ActivityWdtjBinding
import com.kiln.juxingqs.ui.vm.WdtjViewModel
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

//我的竞拍

class WdtjActivity :
    BaseMiddleActivity<WdtjViewModel, ActivityWdtjBinding>() {
    override fun getLayout(): Int {
        return R.layout.activity_wdtj
    }

    val adapter: WdtjAdapter by lazy {
        WdtjAdapter().apply {
            refreshLoading = {
                if (itemCount == 0) {
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_LOADING
                }
            }
            refreshNotLoading = {
                binding.refreshLayout.isRefreshing = false
                if (itemCount == 0) {
                    binding.emptyLayout.errorType = EmptyLayout.NODATA
                } else {
                    binding.emptyLayout.visibility = View.GONE
                }
            }
            refreshError = {
                if (itemCount == 0)
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                toast((it).error.message)
            }
        }
    }

    override fun initViewModel(): WdtjViewModel = createViewModel()

    override fun initView() {
        binding.event = this
        binding.vm = viewModel
        binding.emptyLayout.setOnClickListener { adapter.refresh() }

        binding.refreshLayout.setOnRefreshListener { adapter.refresh() }
        binding.recyclerView.adapter = adapter
        lifecycleScope.launch {
            viewModel.data.collect {
                adapter.submitData(it)
            }
        }

//        viewModel.querenOrder()
    }

}